﻿using MyBlog.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyBlog.Repository
{
    public class BlogRepository : IRepository<Blog>
    {
        private BlogDBContext _dbContext;
        public BlogRepository(BlogDBContext dbContext)
        {
            _dbContext = dbContext;
        }
        public bool Add(Blog entity)
        {
            _dbContext.Blogs.Add(entity);
            int flag = _dbContext.SaveChanges();
            return flag > 0 ? true : false;
        }

        public bool Delete(int id)
        {
            var blog = _dbContext.Blogs.Find(id);
            if (blog != null)
                _dbContext.Blogs.Remove(blog);
            int flag = _dbContext.SaveChanges();
            return flag > 0 ? true : false;
        }

        public Blog? Get(int id)
        {
            return _dbContext.Blogs.Find(id);
        }

        public List<Blog> GetAll()
        {
            return _dbContext.Blogs.ToList();
        }

        public bool Update(Blog entity)
        {
            var mBlog = _dbContext.Blogs.Attach(entity); ;
            mBlog.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            int flag = _dbContext.SaveChanges();
            return flag > 0 ? true : false;
        }
    }
}
